// 接口请求封装
import config from "./url.js";
const baseUrl = config.baseUrl; 

export const request = (url, method, data, callback, contentype) => {
	let token = uni.getStorageSync('yyzyToken') ? uni.getStorageSync('yyzyToken') : ''
	uni.request({
		url: baseUrl + url,
		data: data,
		method: method,
		header: {
			'token': token,
			'content-type': contentype ? contentype : 'application/x-www-form-urlencoded',
		},
		success: (response) => {
			const result = response.data;
			uni.hideLoading();
			
			if(result.code == 1){
				callback(result);
				
			}else if(result.code==401){
				uni.clearStorageSync();
				uni.showToast({
					title: result.msg,
					icon: 'none'
				});
				setTimeout(()=>{
					uni.navigateTo({
						url:"/login/pages/login"
					})
				},1200)
				return;
				
			}else{
				uni.showToast({
					title: result.msg,
					icon: 'none'
				});
				return;
			}
		},
		complete: () => {
			// // uni.hideLoading();
		},
		fail: (error) => {
			if (error) {
				console.log(error)
			}
		}
	})
}

// 调用方法
// this.$api.request('', 'POST',{}, res => {
// 	if (res.code === 200) {
		
// 	} else {
// 		this.toast(res.msg)
// 	}
// });